<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>不定宽高垂直水平居中</title>
  <style>
    html,
    body {
      margin: 0;
      padding: 10px;
      width: 100%;
      height: 100%;
      box-sizing: border-box;
    }

    /* 第一种flex */
    /* .box{
      width:100%;
      height:100%;
      display:flex;
      justify-content:center;
      align-items:center;
    }
    .item {
      width:100px;
      height:100px;
      background-color:blue;
    } */

    /*第二种父级 display:table-cell + 子级 vertical-align: middle */
    /* .box {
      border: 2px solid #ccc;
      padding:10px;
      display: table-cell;  
      margin: 0 auto;
    }

    .item1 {
      width: 100px;
      height: 100px;
      display:inline-block;
      vertical-align: middle;
      background-color: blue;
    }
    .item2 {
      width: 50px;
      height: 50px;
      display:inline-block;
      vertical-align: middle;
      background-color: red;
    } */

    /*第三种 position+transform: translate(-50%,-50%) */
    /* .box {
      width: 100%;
      height: 100%;
      position: relative;
    }
    .item {
      position:absolute;
      left: 50%;
      width:100px;
      height:100px;
      top: 50%;
      transform: translate(-50%,-50%);
      background-color: red;
    } */


    /*第四种  display:flex +margin */
    /*display的属性选 grid,flex,inline-flex,inline-grid 都可 
      inline-flex为行内弹性布局;
      flex和inline-flex的区别:flex宽度默认100%;inline-flex宽度默认子元素宽度(自适应)
      inline-grid和grid同理
      */
    /* .box {
      display: flex;
      width: 100%;
      height: 100%;
    }

    .item {
      width: 100px;
      height: 100px;
      background: burlywood;
      margin: auto;
    } */

    /*第五种 position+margin */
    /* .box {
      width: 100%;
      height: 100%;
      position: relative;
    }
    .item {
      position:absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      margin: auto;
      width: 100px;
      height: 100px;
      background: burlywood;
    } */

    /* 第六种 grid */
    /* .box {
      width: 100%;
      height: 100%;
      display: grid;
    }
    .item {
      width: 100px;
      height: 100px;
      align-self: center;
      justify-self: center;
      background: burlywood;
    } */


    /*第七种 ::after */
    /* .box {
      width: 100%;
      height: 100%;
      text-align: center;
    }

    .box::after {
      content: '';
      display: inline-block;
      vertical-align: middle;
      height: 100%;
    }

    .item {
      display: inline-block;
      background: red;
    } */

    /* 将after替换成before */
     /* .box {
      width: 100%;
      height: 100%;
      text-align: center;
    }

    .box::before {
      content: '';
      display: inline-block;
      vertical-align: middle;
      height: 100%;
    }

    .item {
      display: inline-block;
      background: blue;
    } */
  </style>
</head>

<body>
  <!-- 第一种 flex -->
  <!-- <div class="box">
     <div class="item"></div>
   </div> -->

  <!-- 第二种 父级 display:table-cell + 子级 vertical-align: middle -->
  <!-- <div class="box">
    <div class="item1"></div>
    <div class="item2"></div>
  </div> -->

  <!--第三种  position+transform: translate(-50%,-50%)  -->
  <!-- <div class="box">
     <div class="item"></div>
   </div> -->


  <!--第四种  display:flex +margin -->
  <!-- <div class="box">
    <div class="item"></div>
  </div> -->

  <!-- 第五种 position+margin  -->
  <!-- <div class="box">
    <div class="item"></div>
  </div>  -->

  <!-- 第六种 grid  -->
  <!-- <div class="box">
    <div class="item"></div>
  </div> -->

  <!-- 第七种 ::after  -->
  <!-- <div class="box">
    <div class="item">p</div>
  </div> -->

  <!-- 将after替换成before -->
  <!-- <div class="box">
    <div class="item">p</div>
  </div> -->
</body>

</html>